package com.snow.starter;

import cn.hutool.core.collection.CollUtil;
import com.snow.common.repository.SystemLogRepository;
import com.snow.service.IAuthComponent;
import com.snow.starter.global.AccessFilter;
import com.snow.starter.global.GlobalExceptionHandler;
import com.snow.starter.global.SnowWebConfiguration;
import com.snow.starter.interceptor.AuthInterceptor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import java.util.List;

/**
 * snow 自动配置
 *
 * @author by 13119
 */
@Slf4j
@Getter
@Setter
@Configuration
@EntityScan({"com.snow.auth.entity","com.snow.common.entity"})
@EnableJpaRepositories({"com.snow.auth.repository","com.snow.common.repository"})
@ComponentScan(value = {
        "com.snow.auth",
        "com.snow.common",
        "com.snow.component",
        "com.snow.wechat",
})
@ConfigurationProperties("snow")
public class SnowAutoConfiguration {

    public SnowAutoConfiguration() {
        log.debug("SnowAutoConfiguration starting configuration");
    }

    /**
     * 是否启动访问过滤器，建议启动
     */
    private Boolean enableAccessFilter = true;
    /**
     * 是否启动全局webConfig
     */
    private Boolean enableGlobalWebConfig = true;
    /**
     * 是否启动全局异常拦截器
     */
    private Boolean enableGlobalException = true;
    /**
     * 是否启动签名校验
     */
    private Boolean enableSignValidate = true;
    /**
     * 是否开启租户过期功能
     */
    private Boolean enableTenantExpired = true;
    /**
     * 默认密码
     */
    private String defaultPwd = "3edc#EDC";
    /**
     * 网站域名
     */
    private String webDomain = "snow-momo.top";
    /**
     * 域名协议
     */
    private String webScheme = "https";
    private List<String> handlerPaths = CollUtil.newArrayList("/**");
    private List<String> handlerExceptPaths = CollUtil.newArrayList("/login", "/error", "/static-resources/**");
    /**
     * 文件存储方式
     * local 硬盘存储
     * minio minio云存储(默认)
     */
    private String fileStorageType = "minio";
    private String minioEndpoint = "http://127.0.0.1:9000";
    private String minioPreviewPrefix = "";
    /**
     * minioAccessKey 用户名
     */
    private String minioAccessKey = "admin";
    /**
     * minioSecretKey 密码
     */
    private String minioSecretKey = "123456";
    /**
     * 存储桶名称
     */
    private String bucketName = "common-bucket";

    /**
     * 永久桶
     */
    private String publicBucket = "public-bucket";
    /**
     * 文件保存地址，/开头
     */
    private String fileSavePath = "/snow/annex/";
    private String filePreviewPrefix = "/api/";

    @Bean
    @ConditionalOnProperty(value = "snow.enable-access-filter", havingValue = "true", matchIfMissing = true)
    public AccessFilter accessFilter(SystemLogRepository systemLogRepository, @Autowired(required = false) IAuthComponent authComponent) {
        log.info("AccessFilter过滤器配置已开启");
        return new AccessFilter(systemLogRepository, authComponent, this);
    }

    @Bean
    @ConditionalOnProperty(value = "snow.enable-global-web-config", havingValue = "true", matchIfMissing = true)
    public SnowWebConfiguration globalWebConfiguration(@Autowired(required = false) AuthInterceptor authInterceptor) {
        return new SnowWebConfiguration(authInterceptor, this);
    }

    @Bean
    AuthInterceptor authInterceptor(@Autowired(required = false) IAuthComponent authComponent) {
        log.info("全局拦截器已注入");
        if (authComponent == null) {
            log.warn("未加载到IAuthComponent组件，将跳过登录及权限校验，系统存在攻击风险，建议联系管理员处理");
        }
        return new AuthInterceptor(authComponent);
    }

    @Bean
    public GlobalExceptionHandler globalExceptionHandler() {
        log.info("全局异常处理器已注入");
        return new GlobalExceptionHandler();
    }


}
